home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0009_MAZE.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  2KB  |  67 lines

  1. {
  2. SEAN PALMER
  3.  
  4. > Hello there.. I was just wondering.. Since I am completely 'C'
  5. > illiterate, could someone please make an effort and convert the
  6. > following code in Pascal For me? (Its supposedly makes a solveable
  7. > maze every time, Cool)
  8.  
  9. {originally by jallen@ic.sunysb.edu}
  10. {Turbo Pascal conversion by Sean Palmer from original C}
  11.  
  12. Const
  13.   h = 23; {height}
  14.   w = 79; {width}
  15.  
  16. Const
  17.   b : Array [0..3] of Integer = (-w, w, 1, -1);
  18.   { incs For up, down, right, left }
  19.  
  20. Var
  21.   a : Array [0..w * h - 1] of Boolean;  { the maze (False = wall) }
  22.  
  23. Procedure m(p : Integer);
  24. Var
  25.   i, d : Byte;
  26. begin
  27.   a[p] := True;           {make a path}
  28.   Repeat
  29.     d := 0;               {check For allowable directions}
  30.     if (p > 2 * w) and not (a[p - w - w]) then
  31.       inc(d, 1);          {up}
  32.     if (p < w * (h - 2)) and not (a[p + w + w]) then
  33.       inc(d, 2);          {down}
  34.     if (p mod w <> w - 2) and not (a[p + 2]) then
  35.       inc(d, 4);          {right}
  36.     if (p mod w <> 1) and not (a[p - 2]) then
  37.       inc(d, 8);          {left}
  38.     if d <> 0 then
  39.     begin
  40.       Repeat              {choose a direction that's legal}
  41.         i := random(4);
  42.       Until Boolean(d and(1 shl i));
  43.  
  44.      a[p + b[i]] := True; {make a path}
  45.      m(p + 2 * b[i]);     {recurse}
  46.     end;
  47.   Until d = 0;            {Until stuck}
  48. end;
  49.  
  50. Var
  51.   i : Integer;
  52.  
  53. begin
  54.   randomize;
  55.   fillChar(a, sizeof(a), False);
  56.   m(succ(w));  {start at upper left}
  57.   For i := 0 to pred(w * h) do
  58.   begin {draw}
  59.     if i mod w = 0 then
  60.       Writeln;
  61.     if a[i] then
  62.       Write(' ')
  63.     else
  64.       Write('█');
  65.   end;
  66. end.
  67.